﻿Imports System.Net
Imports System.IO

Public Class Utils

  Public Enum eColumnCaseDefinition
    ToUpper = 1
    ToLower = 2
    DBDefault = 0
  End Enum

  <Obsolete("Diese Funktion sollte nicht länger verwendet werden")> _
  Public Shared Function GetArrayListFromReader(ByVal reader As System.Data.Common.DbDataReader, ByVal ccd As eColumnCaseDefinition) As ArrayList
    Dim result As New ArrayList
    Dim ucase As String

    Dim i As Integer

    While reader.Read()
      Dim spalte As New Hashtable
      i = 0
      For j As Integer = 1 To reader.VisibleFieldCount Step 1
        ucase = reader.GetName(i)
        Select Case ccd
          Case eColumnCaseDefinition.ToUpper
            ucase = ucase.ToUpper
          Case eColumnCaseDefinition.ToLower
            ucase = ucase.ToLower
          Case eColumnCaseDefinition.DBDefault
            ucase = ucase
        End Select
        If reader(i).GetType.ToString = "System.DBNull" Then
          spalte.Add(ucase, Nothing)
        Else
          spalte.Add(ucase, reader(i))
        End If

        i += 1
      Next
      result.Add(spalte)
    End While
    reader.Close()
    Return result

  End Function

  Public Shared ReadOnly Property GetPreviewImage(ByVal w As Wallpaper) As Image
    Get
      Dim wr As HttpWebRequest = _
      CType(WebRequest.Create("http://betathumbnails.skins.be/" & w.Name & "/" & w.Name & "-" & w.OrgaID & ".jpg"), HttpWebRequest)
      Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse)
      Dim str As Stream = ws.GetResponseStream()
      Return Image.FromStream(str)
    End Get
  End Property

  Public Shared Function InitCap(ByVal s As String) As String
    Dim re As String = ""
    Dim r() As String
    r = s.Split(" ")
    For i As Integer = 0 To r.Length - 1 Step 1
      Try
        re &= r(i).Substring(0, 1).ToUpper & r(i).Substring(1).ToLower & " "
      Catch ex As Exception
      End Try
    Next
    Return re.Trim()
  End Function

  Public Shared Function DbNull2Nothing(value As Object) As Object
  	If value Is Nothing Then
  		Return System.DBNull.Value
  	Else
  		Return value
  	End If
  End Function
End Class
